#include "MainWindow.h"
#include "ui_MainWindow.h"


void MainWindow::keyPressEvent(QKeyEvent *event){
    switch(event->key()){
    case Qt::Key_1:{
        isActive[12]=1;
        emit mySignal();
        connect(this,SIGNAL(mySignal()),scr,SLOT(scrSlot()));

        QMediaPlayer *player=new QMediaPlayer;
        player->setMedia(QMediaContent(QUrl("qrc:/media/sound/C3.wav")));//实测wav文件比mp3延迟低
        if(event->isAutoRepeat())//防止重复按键
           break;
        player->play();

        ui->btn_C3->setStyleSheet("QPushButton{background-color: rgb(245, 245, 245);border:2px groove gray;border-radius:10px;padding:2px 4px;border-style: outset;}");
        break;
    }
    case Qt::Key_2:{
        isActive[13]=1;
        emit mySignal();
        connect(this,SIGNAL(mySignal()),scr,SLOT(scrSlot()));

        QMediaPlayer *player=new QMediaPlayer;
        player->setMedia(QMediaContent(QUrl("qrc:/media/sound/C3up.wav")));
        if(event->isAutoRepeat())
            break;
        player->play();
        ui->btn_C3up->setStyleSheet("QPushButton{background-color: rgb(76, 76, 76);border:2px groove gray;border-radius:10px;padding:2px 4px;border-style: outset;}");

        break;
    }
    case Qt::Key_3:{
        isActive[14]=1;
        emit mySignal();
        connect(this,SIGNAL(mySignal()),scr,SLOT(scrSlot()));

        QMediaPlayer *player=new QMediaPlayer;
        player->setMedia(QMediaContent(QUrl("qrc:/media/sound/D3.wav")));//实测wav文件比mp3延迟低
        if(event->isAutoRepeat())//防止重复按键
           break;
        player->play();

        ui->btn_D3->setStyleSheet("QPushButton{background-color: rgb(245, 245, 245);border:2px groove gray;border-radius:10px;padding:2px 4px;border-style: outset;}");
        break;
    }
    case Qt::Key_4:{
        isActive[15]=1;
        emit mySignal();
        connect(this,SIGNAL(mySignal()),scr,SLOT(scrSlot()));

        QMediaPlayer *player=new QMediaPlayer;
        player->setMedia(QMediaContent(QUrl("qrc:/media/sound/D3up.wav")));
        if(event->isAutoRepeat())
            break;
        player->play();
        ui->btn_D3up->setStyleSheet("QPushButton{background-color: rgb(76, 76, 76);border:2px groove gray;border-radius:10px;padding:2px 4px;border-style: outset;}");

        break;
    }
    case Qt::Key_5:{
        isActive[16]=1;
        emit mySignal();
        connect(this,SIGNAL(mySignal()),scr,SLOT(scrSlot()));

        QMediaPlayer *player=new QMediaPlayer;
        player->setMedia(QMediaContent(QUrl("qrc:/media/sound/E3.wav")));//实测wav文件比mp3延迟低
        if(event->isAutoRepeat())//防止重复按键
           break;
        player->play();

        ui->btn_E3->setStyleSheet("QPushButton{background-color: rgb(245, 245, 245);border:2px groove gray;border-radius:10px;padding:2px 4px;border-style: outset;}");
        break;
    }
    case Qt::Key_6:{
        isActive[17]=1;
        emit mySignal();
        connect(this,SIGNAL(mySignal()),scr,SLOT(scrSlot()));

        QMediaPlayer *player=new QMediaPlayer;
        player->setMedia(QMediaContent(QUrl("qrc:/media/sound/F3.wav")));//实测wav文件比mp3延迟低
        if(event->isAutoRepeat())//防止重复按键
           break;
        player->play();

        ui->btn_F3->setStyleSheet("QPushButton{background-color: rgb(245, 245, 245);border:2px groove gray;border-radius:10px;padding:2px 4px;border-style: outset;}");
        break;
    }
    case Qt::Key_7:{
        isActive[18]=1;
        emit mySignal();
        connect(this,SIGNAL(mySignal()),scr,SLOT(scrSlot()));

        QMediaPlayer *player=new QMediaPlayer;
        player->setMedia(QMediaContent(QUrl("qrc:/media/sound/F3up.wav")));
        if(event->isAutoRepeat())
            break;
        player->play();
        ui->btn_F3up->setStyleSheet("QPushButton{background-color: rgb(76, 76, 76);border:2px groove gray;border-radius:10px;padding:2px 4px;border-style: outset;}");

        break;
    }
    case Qt::Key_8:{
        isActive[19]=1;
        emit mySignal();
        connect(this,SIGNAL(mySignal()),scr,SLOT(scrSlot()));

        QMediaPlayer *player=new QMediaPlayer;
        player->setMedia(QMediaContent(QUrl("qrc:/media/sound/G3.wav")));
        if(event->isAutoRepeat())//防止重复按键
           break;
        player->play();

        ui->btn_G3->setStyleSheet("QPushButton{background-color: rgb(245, 245, 245);border:2px groove gray;border-radius:10px;padding:2px 4px;border-style: outset;}");
        break;
    }
    case Qt::Key_9:{
        isActive[20]=1;
        emit mySignal();
        connect(this,SIGNAL(mySignal()),scr,SLOT(scrSlot()));

        QMediaPlayer *player=new QMediaPlayer;
        player->setMedia(QMediaContent(QUrl("qrc:/media/sound/G3up.wav")));
        if(event->isAutoRepeat())
            break;
        player->play();
        ui->btn_G3up->setStyleSheet("QPushButton{background-color: rgb(76, 76, 76);border:2px groove gray;border-radius:10px;padding:2px 4px;border-style: outset;}");

        break;
    }
    case Qt::Key_0:{
        isActive[21]=1;
        emit mySignal();
        connect(this,SIGNAL(mySignal()),scr,SLOT(scrSlot()));

        QMediaPlayer *player=new QMediaPlayer;
        player->setMedia(QMediaContent(QUrl("qrc:/media/sound/A3.wav")));
        if(event->isAutoRepeat())
           break;
        player->play();

        ui->btn_A3->setStyleSheet("QPushButton{background-color: rgb(245, 245, 245);border:2px groove gray;border-radius:10px;padding:2px 4px;border-style: outset;}");
        break;
    }
    case Qt::Key_Minus:{
        isActive[22]=1;
        emit mySignal();
        connect(this,SIGNAL(mySignal()),scr,SLOT(scrSlot()));

        QMediaPlayer *player=new QMediaPlayer;
        player->setMedia(QMediaContent(QUrl("qrc:/media/sound/A3up.wav")));
        if(event->isAutoRepeat())
            break;
        player->play();
        ui->btn_A3up->setStyleSheet("QPushButton{background-color: rgb(76, 76, 76);border:2px groove gray;border-radius:10px;padding:2px 4px;border-style: outset;}");

        break;
    }
    case Qt::Key_Equal:{
        isActive[23]=1;
        emit mySignal();
        connect(this,SIGNAL(mySignal()),scr,SLOT(scrSlot()));

        QMediaPlayer *player=new QMediaPlayer;
        player->setMedia(QMediaContent(QUrl("qrc:/media/sound/B3.wav")));
        if(event->isAutoRepeat())
           break;
        player->play();

        ui->btn_B3->setStyleSheet("QPushButton{background-color: rgb(245, 245, 245);border:2px groove gray;border-radius:10px;padding:2px 4px;border-style: outset;}");
        break;
    }
    case Qt::Key_Q:{
        isActive[24]=1;
        emit mySignal();
        connect(this,SIGNAL(mySignal()),scr,SLOT(scrSlot()));

        QMediaPlayer *player=new QMediaPlayer;
        player->setMedia(QMediaContent(QUrl("qrc:/media/sound/C4.wav")));
        if(event->isAutoRepeat())//防止重复按键
           break;
        player->play();

        ui->btn_C4->setStyleSheet("QPushButton{background-color: rgb(245, 245, 245);border:2px groove gray;border-radius:10px;padding:2px 4px;border-style: outset;}");
        break;
    }
    case Qt::Key_W:{
        isActive[25]=1;
        emit mySignal();
        connect(this,SIGNAL(mySignal()),scr,SLOT(scrSlot()));

        QMediaPlayer *player=new QMediaPlayer;
        player->setMedia(QMediaContent(QUrl("qrc:/media/sound/C4up.wav")));
        if(event->isAutoRepeat())
            break;
        player->play();
        ui->btn_C4up->setStyleSheet("QPushButton{background-color: rgb(76, 76, 76);border:2px groove gray;border-radius:10px;padding:2px 4px;border-style: outset;}");

        break;
    }
    case Qt::Key_E:{
        isActive[26]=1;
        QMediaPlayer *player=new QMediaPlayer;
        emit mySignal();
        connect(this,SIGNAL(mySignal()),scr,SLOT(scrSlot()));

        player->setMedia(QMediaContent(QUrl("qrc:/media/sound/D4.wav")));
        if(event->isAutoRepeat())
            break;
        player->play();
         ui->btn_D4->setStyleSheet("QPushButton{background-color: rgb(240, 240, 240);border:2px groove gray;border-radius:10px;padding:2px 4px;border-style: outset;}");
        break;
    }
    case Qt::Key_R:{
        isActive[27]=1;
        QMediaPlayer *player=new QMediaPlayer;
        emit mySignal();
        connect(this,SIGNAL(mySignal()),scr,SLOT(scrSlot()));
        player->setMedia(QMediaContent(QUrl("qrc:/media/sound/D4up.wav")));
        if(event->isAutoRepeat())
            break;
        player->play();
          ui->btn_D4up->setStyleSheet("QPushButton{background-color: rgb(76, 76, 76);border:2px groove gray;border-radius:10px;padding:2px 4px;border-style: outset;}");
        break;
    }
    case Qt::Key_T:{
        isActive[28]=1;
        QMediaPlayer *player=new QMediaPlayer;
        emit mySignal();
        connect(this,SIGNAL(mySignal()),scr,SLOT(scrSlot()));
        player->setMedia(QMediaContent(QUrl("qrc:/media/sound/E4.wav")));
        if(event->isAutoRepeat())
            break;
        player->play();
         ui->btn_E4->setStyleSheet("QPushButton{background-color: rgb(240, 240, 240);border:2px groove gray;border-radius:10px;padding:2px 4px;border-style: outset;}");
        break;
    }
    case Qt::Key_Y:{
        isActive[29]=1;
        QMediaPlayer *player=new QMediaPlayer;
        emit mySignal();
        connect(this,SIGNAL(mySignal()),scr,SLOT(scrSlot()));
        player->setMedia(QMediaContent(QUrl("qrc:/media/sound/F4.wav")));
        if(event->isAutoRepeat())
            break;
        player->play();
         ui->btn_F4->setStyleSheet("QPushButton{background-color: rgb(240, 240, 240);border:2px groove gray;border-radius:10px;padding:2px 4px;border-style: outset;}");
        break;
    }
    case Qt::Key_U:{
        isActive[30]=1;
        QMediaPlayer *player=new QMediaPlayer;
        emit mySignal();
        connect(this,SIGNAL(mySignal()),scr,SLOT(scrSlot()));
        player->setMedia(QMediaContent(QUrl("qrc:/media/sound/F4up.wav")));
        if(event->isAutoRepeat())
            break;
        player->play();
          ui->btn_F4up->setStyleSheet("QPushButton{background-color: rgb(76, 76, 76);border:2px groove gray;border-radius:10px;padding:2px 4px;border-style: outset;}");
        break;
    }
    case Qt::Key_I:{
        isActive[31]=1;
        QMediaPlayer *player=new QMediaPlayer;
        emit mySignal();
        connect(this,SIGNAL(mySignal()),scr,SLOT(scrSlot()));
        player->setMedia(QMediaContent(QUrl("qrc:/media/sound/G4.wav")));
        if(event->isAutoRepeat())
            break;
        player->play();
         ui->btn_G4->setStyleSheet("QPushButton{background-color: rgb(240, 240, 240);border:2px groove gray;border-radius:10px;padding:2px 4px;border-style: outset;}");
        break;
    }
    case Qt::Key_O:{
        isActive[32]=1;
        QMediaPlayer *player=new QMediaPlayer;
        emit mySignal();
        connect(this,SIGNAL(mySignal()),scr,SLOT(scrSlot()));
        player->setMedia(QMediaContent(QUrl("qrc:/media/sound/G4up.wav")));
        if(event->isAutoRepeat())
            break;
        player->play();
          ui->btn_G4up->setStyleSheet("QPushButton{background-color: rgb(76, 76, 76);border:2px groove gray;border-radius:10px;padding:2px 4px;border-style: outset;}");
        break;
    }
    case Qt::Key_P:{
        isActive[33]=1;
        QMediaPlayer *player=new QMediaPlayer;
        emit mySignal();
        connect(this,SIGNAL(mySignal()),scr,SLOT(scrSlot()));
        player->setMedia(QMediaContent(QUrl("qrc:/media/sound/A4.wav")));
        if(event->isAutoRepeat())
            break;
        player->play();
         ui->btn_A4->setStyleSheet("QPushButton{background-color: rgb(240, 240, 240);border:2px groove gray;border-radius:10px;padding:2px 4px;border-style: outset;}");
        break;
    }
    case Qt::Key_BracketLeft:{
        isActive[34]=1;
        QMediaPlayer *player=new QMediaPlayer;
        emit mySignal();
        connect(this,SIGNAL(mySignal()),scr,SLOT(scrSlot()));
        player->setMedia(QMediaContent(QUrl("qrc:/media/sound/A4up.wav")));
        if(event->isAutoRepeat())
            break;
        player->play();
          ui->btn_A4up->setStyleSheet("QPushButton{background-color: rgb(76, 76, 76);border:2px groove gray;border-radius:10px;padding:2px 4px;border-style: outset;}");
        break;
    }
    case Qt::Key_BracketRight:{
        isActive[35]=1;
        QMediaPlayer *player=new QMediaPlayer;
        emit mySignal();
        connect(this,SIGNAL(mySignal()),scr,SLOT(scrSlot()));
        player->setMedia(QMediaContent(QUrl("qrc:/media/sound/B4.wav")));
        if(event->isAutoRepeat())
            break;
        player->play();
         ui->btn_B4->setStyleSheet("QPushButton{background-color: rgb(240, 240, 240);border:2px groove gray;border-radius:10px;padding:2px 4px;border-style: outset;}");
        break;
    }

    default:;
    }

}

void MainWindow::keyReleaseEvent(QKeyEvent *event){
    switch(event->key()){
    case Qt::Key_1:{
        isActive[12]=0;

        emit mySignal();
        connect(this,SIGNAL(mySignal()),scr,SLOT(scrSlot()));
        ui->btn_C3->setStyleSheet("QPushButton{background-color: rgb(234, 234, 234);border:2px groove gray;border-radius:10px;padding:2px 4px;border-style: outset;}");
   break;
    }
    case Qt::Key_2:{
        isActive[13]=0;

        emit mySignal();
        connect(this,SIGNAL(mySignal()),scr,SLOT(scrSlot()));
        ui->btn_C3up->setStyleSheet("QPushButton{background-color: rgb(54, 54, 54);border:2px groove gray;border-radius:10px;padding:2px 4px;border-style: outset;}");
        break;
    }
    case Qt::Key_3:{
        isActive[14]=0;

        emit mySignal();
        connect(this,SIGNAL(mySignal()),scr,SLOT(scrSlot()));
        ui->btn_D3->setStyleSheet("QPushButton{background-color: rgb(234, 234, 234);border:2px groove gray;border-radius:10px;padding:2px 4px;border-style: outset;}");
   break;
    }
    case Qt::Key_4:{
        isActive[15]=0;

        emit mySignal();
        connect(this,SIGNAL(mySignal()),scr,SLOT(scrSlot()));
        ui->btn_D3up->setStyleSheet("QPushButton{background-color: rgb(54, 54, 54);border:2px groove gray;border-radius:10px;padding:2px 4px;border-style: outset;}");
        break;
    }
    case Qt::Key_5:{
        isActive[16]=0;

        emit mySignal();
        connect(this,SIGNAL(mySignal()),scr,SLOT(scrSlot()));
        ui->btn_E3->setStyleSheet("QPushButton{background-color: rgb(234, 234, 234);border:2px groove gray;border-radius:10px;padding:2px 4px;border-style: outset;}");
   break;
    }
    case Qt::Key_6:{
        isActive[17]=0;

        emit mySignal();
        connect(this,SIGNAL(mySignal()),scr,SLOT(scrSlot()));
        ui->btn_F3->setStyleSheet("QPushButton{background-color: rgb(234, 234, 234);border:2px groove gray;border-radius:10px;padding:2px 4px;border-style: outset;}");
   break;
    }
    case Qt::Key_7:{
        isActive[18]=0;

        emit mySignal();
        connect(this,SIGNAL(mySignal()),scr,SLOT(scrSlot()));
        ui->btn_F3up->setStyleSheet("QPushButton{background-color: rgb(54, 54, 54);border:2px groove gray;border-radius:10px;padding:2px 4px;border-style: outset;}");
        break;
    }
    case Qt::Key_8:{
        isActive[19]=0;

        emit mySignal();
        connect(this,SIGNAL(mySignal()),scr,SLOT(scrSlot()));
        ui->btn_G3->setStyleSheet("QPushButton{background-color: rgb(234, 234, 234);border:2px groove gray;border-radius:10px;padding:2px 4px;border-style: outset;}");
   break;
    }
    case Qt::Key_9:{
        isActive[20]=0;

        emit mySignal();
        connect(this,SIGNAL(mySignal()),scr,SLOT(scrSlot()));
        ui->btn_G3up->setStyleSheet("QPushButton{background-color: rgb(54, 54, 54);border:2px groove gray;border-radius:10px;padding:2px 4px;border-style: outset;}");
        break;
    }
    case Qt::Key_0:{
        isActive[21]=0;

        emit mySignal();
        connect(this,SIGNAL(mySignal()),scr,SLOT(scrSlot()));
        ui->btn_A3->setStyleSheet("QPushButton{background-color: rgb(234, 234, 234);border:2px groove gray;border-radius:10px;padding:2px 4px;border-style: outset;}");
   break;
    }
    case Qt::Key_Minus:{
        isActive[22]=0;

        emit mySignal();
        connect(this,SIGNAL(mySignal()),scr,SLOT(scrSlot()));
        ui->btn_A3up->setStyleSheet("QPushButton{background-color: rgb(54, 54, 54);border:2px groove gray;border-radius:10px;padding:2px 4px;border-style: outset;}");
        break;
    }
    case Qt::Key_Equal:{
        isActive[23]=0;

        emit mySignal();
        connect(this,SIGNAL(mySignal()),scr,SLOT(scrSlot()));
        ui->btn_B3->setStyleSheet("QPushButton{background-color: rgb(234, 234, 234);border:2px groove gray;border-radius:10px;padding:2px 4px;border-style: outset;}");
   break;
    }
    case Qt::Key_Q:{
        isActive[24]=0;

        emit mySignal();
        connect(this,SIGNAL(mySignal()),scr,SLOT(scrSlot()));
        ui->btn_C4->setStyleSheet("QPushButton{background-color: rgb(234, 234, 234);border:2px groove gray;border-radius:10px;padding:2px 4px;border-style: outset;}");
   break;
    }
    case Qt::Key_W:{
        isActive[25]=0;

        emit mySignal();
        connect(this,SIGNAL(mySignal()),scr,SLOT(scrSlot()));
        ui->btn_C4up->setStyleSheet("QPushButton{background-color: rgb(54, 54, 54);border:2px groove gray;border-radius:10px;padding:2px 4px;border-style: outset;}");
        break;
    }
    case Qt::Key_E:{
        isActive[26]=0;

        emit mySignal();
        connect(this,SIGNAL(mySignal()),scr,SLOT(scrSlot()));
        ui->btn_D4->setStyleSheet("QPushButton{background-color: rgb(234, 234, 234);border:2px groove gray;border-radius:10px;padding:2px 4px;border-style: outset;}");
        break;
    }
    case Qt::Key_R:{
        isActive[27]=0;

        emit mySignal();
        connect(this,SIGNAL(mySignal()),scr,SLOT(scrSlot()));
        ui->btn_D4up->setStyleSheet("QPushButton{background-color: rgb(54, 54, 54);border:2px groove gray;border-radius:10px;padding:2px 4px;border-style: outset;}");
        break;
    }
    case Qt::Key_T:{
        isActive[28]=0;

        emit mySignal();
        connect(this,SIGNAL(mySignal()),scr,SLOT(scrSlot()));
        ui->btn_E4->setStyleSheet("QPushButton{background-color: rgb(234, 234, 234);border:2px groove gray;border-radius:10px;padding:2px 4px;border-style: outset;}");
        break;
    }
    case Qt::Key_Y:{
        isActive[29]=0;

        emit mySignal();
        connect(this,SIGNAL(mySignal()),scr,SLOT(scrSlot()));
        ui->btn_F4->setStyleSheet("QPushButton{background-color: rgb(234, 234, 234);border:2px groove gray;border-radius:10px;padding:2px 4px;border-style: outset;}");
    }
    case Qt::Key_U:{
        isActive[30]=0;

        emit mySignal();
        connect(this,SIGNAL(mySignal()),scr,SLOT(scrSlot()));
        ui->btn_F4up->setStyleSheet("QPushButton{background-color: rgb(54, 54, 54);border:2px groove gray;border-radius:10px;padding:2px 4px;border-style: outset;}");
        break;
    }
    case Qt::Key_I:{
        isActive[31]=0;

        emit mySignal();
        connect(this,SIGNAL(mySignal()),scr,SLOT(scrSlot()));
        ui->btn_G4->setStyleSheet("QPushButton{background-color: rgb(234, 234, 234);border:2px groove gray;border-radius:10px;padding:2px 4px;border-style: outset;}");
        break;
    }
    case Qt::Key_O:{
        isActive[32]=0;

        emit mySignal();
        connect(this,SIGNAL(mySignal()),scr,SLOT(scrSlot()));
        ui->btn_G4up->setStyleSheet("QPushButton{background-color: rgb(54, 54, 54);border:2px groove gray;border-radius:10px;padding:2px 4px;border-style: outset;}");
        break;
    }
    case Qt::Key_P:{
        isActive[33]=0;

        emit mySignal();
        connect(this,SIGNAL(mySignal()),scr,SLOT(scrSlot()));
        ui->btn_A4->setStyleSheet("QPushButton{background-color: rgb(234, 234, 234);border:2px groove gray;border-radius:10px;padding:2px 4px;border-style: outset;}");
        break;
    }
    case Qt::Key_BracketLeft:{
        isActive[34]=0;

        emit mySignal();
        connect(this,SIGNAL(mySignal()),scr,SLOT(scrSlot()));
        ui->btn_A4up->setStyleSheet("QPushButton{background-color: rgb(54, 54, 54);border:2px groove gray;border-radius:10px;padding:2px 4px;border-style: outset;}");
        break;
    }
    case Qt::Key_BraceRight:{
        isActive[35]=0;//键盘这个键有问题，没有release event

        emit mySignal();
        connect(this,SIGNAL(mySignal()),scr,SLOT(scrSlot()));
        ui->btn_B4->setStyleSheet("QPushButton{background-color: rgb(234, 234, 234);border:2px groove gray;border-radius:10px;padding:2px 4px;border-style: outset;}");
        break;
    }
      default:;
    }

}
